<?php
namespace app\admin\controller;
use think\Controller;
use think\Request;
use think\Verify;
// use app\admin\model\Config as ConfigModel;
/**
 * 后台登录控制器
 * @author 唐小虎 <1731050423@qq.com>
 */
class Account extends Controller {
	/**
     * 后台用户登录
     * @author 唐小虎 <1731050423@qq.com>
     */
	public function login($username = null, $password = null, $verify = null){
		if(request()->isPost()){
			/* 检测验证码 TODO: */
            // if(!captcha_check($verify)){
            //     $this->error('验证码输入错误！');
            // };
			/* 调用登录接口登录 */
            $account = new \app\admin\model\Admin;

            $uid = $account->login($username, $password);
            if(0 < $uid){ //UC登录成功
                //记录行为
		        action_log('user_login', 'admin', $uid, $uid);
                $this->success('登录成功','/admin/Index/index');
            } else { //登录失败
                switch($uid) {
                    case -1: $error = '用户不存在或被禁用！'; break; //系统级别禁用
                    case -2: $error = '密码错误！'; break;
                    default: $error = '未知错误！'; break; // 0-接口参数错误（调试阶段使用）
                }
                $this->error($error,'/admin/login/login');
            }
		} else {
            if(is_login()){
                $this->redirect('/admin/Index/index');
            }else{
                /* 读取数据库中的配置 */
                // $config	=	cache('SYSTEM_CONFIG');
                // if(!$config){
                //     $config	=	ConfigModel::lists();
                //     cache('SYSTEM_CONFIG',$config,$config);
                // }
                // config('SYSTEM_CONFIG',$config); //添加配置
                return $this->fetch();
            }
        }
	}

	/* 退出登录 */
    public function signout(){
        if(is_login()){
            $admin = new \app\admin\model\Admin;
            $admin->logout();
            session('[destroy]');
            $this->success('退出成功！', url('/admin/account/login'));
        } else {
            $this->redirect('/admin/account/login');
        }
    }

    /**
     * 验证码
     * @AuthorHTL
     * @DateTime  2016-11-25T17:23:47+0800
     * @return    [type]                   [description]
     */
    public function vertify()
    {
        $verify = new \org\Verify();
        $verify->codeSet = '0123456789';         
        $verify->imageH = 30;
        $verify->imageW = 100;
        $verify->fontSize = 16;
        $verify->length   = 3;
        $verify->useCurve   = false;
        ob_clean();
        $verify->entry();
    }
}
